package com.example.toolsdemo.controller;

import com.example.toolsdemo.common.ServerResponse;
import com.example.toolsdemo.dataobject.Student;
import com.example.toolsdemo.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author danyubin
 */
@Api(value = "学生信息", tags = {"学生信息的相关接口"})
@RestController
@RequestMapping("/api/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "根据学生id查询学生", notes = "根据学生id查询学生", httpMethod = "GET")
    @GetMapping("/get")
    public ServerResponse<Student> get(@RequestParam("id") Long id) {
        Student student = studentService.getStudent(id);
        return ServerResponse.success(student);
    }

    @ApiOperation(value = "保存学生", notes = "保存学生", httpMethod = "POST")
    @PostMapping("/save")
    public ServerResponse save(@RequestBody Student student) {
        studentService.saveStudent(student);
        return ServerResponse.success("保存成功");
    }

    @ApiOperation(value = "修改学生", notes = "修改学生", httpMethod = "POST")
    @PostMapping("/modify")
    public ServerResponse modify(@RequestBody Student student) {

        return studentService.modifyStudent(student);
    }

    @ApiOperation(value = "删除学生byid", notes = "删除学生byid", httpMethod = "POST")
    @PostMapping("/delete")
    public ServerResponse delete(Long id) {
        Integer integer = studentService.deleteStudent(id);
        return ServerResponse.success();
    }

    @ApiOperation(value = "查询学生列表bystatus分页", notes = "查询学生列表bystatus分页", httpMethod = "GET")
    @GetMapping("/getStudentsByStatus")
    public ServerResponse getStudentsByStatus(@ApiParam(name = "status", value = "学生状态", required = true)
                                                  @RequestParam("status") Integer status,
                                              @ApiParam(name = "pageNum", value = "页号", required = false)
                                              @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
                                              @ApiParam(name = "pageSize", value = "每页大小", required = false)
                                              @RequestParam(value = "pageSize", defaultValue = "1", required = false) Integer pageSize) {

        return studentService.getStudentsByStatus(status, pageNum, pageSize);
    }
}
